package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.config.service.StsService;
import cn.zucc.edu.handyzucc.entity.User;
import cn.zucc.edu.handyzucc.service.ImageService;
import cn.zucc.edu.handyzucc.service.PostService;
import cn.zucc.edu.handyzucc.service.UserService;
import cn.zucc.edu.handyzucc.service.impl.ImageServiceImpl;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private PostService postService;
    @Autowired
    private StsService stsService;
    @Autowired
    private ImageService imageService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    private Map<String, Object> login(String user_username, String user_password) {
        Map<String, Object> modelMap = new HashMap<>();
        User loginUser = userService.login(user_username, user_password);
        if (loginUser != null) {
            modelMap.put("code", "0");
            modelMap.put("data", loginUser);
            modelMap.put("info", "登录成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("data", loginUser);
            modelMap.put("info", "登录失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "signup", method = RequestMethod.GET)
    private Map<String, Object> signup() throws IOException {
        Map<String, Object> modelMap = new HashMap<>();
        FileInputStream in = new FileInputStream("./src/main/resources/assets/zuccicon.jpg");
        imageService.uploadAvatorStream(in,3);
        modelMap.put("code", "0");
        modelMap.put("info", "注册成功");
        return modelMap;
    }

    @RequestMapping(value = "batchSignup", method = RequestMethod.POST)
    private Map<String, Object> batchSignup(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        Map<String, Object> modelMap = new HashMap<>();
        List<User> users = userService.batchSignup(multipartFile);
        modelMap.put("code", "0");
        modelMap.put("data", users);
        return modelMap;

    }

    @RequestMapping(value = "getFollow", method = RequestMethod.POST)
    private Map<String, Object> getFollow(Integer follow_follower) {
        Map<String, Object> modelMap = new HashMap<>();
        List<User> userList = userService.getFollow(follow_follower);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", userList);
        return modelMap;
    }

    @RequestMapping(value = "getFans", method = RequestMethod.POST)
    private Map<String, Object> getFans(Integer follow_leader) {
        Map<String, Object> modelMap = new HashMap<>();
        List<User> userList = userService.getFans(follow_leader);
        List<Boolean> alreadyFollowList = new ArrayList<>();
        for (User user : userList
        ) {
            alreadyFollowList.add(postService.ifAlreadyFollow(user.getUser_id(), follow_leader));
        }
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", userList);
        modelMap.put("follow", alreadyFollowList);
        return modelMap;
    }

    @RequestMapping(value = "getUsersByKeyword", method = RequestMethod.POST)
    private Map<String, Object> getUsersByKeyword(String keyword, Integer follow_follower) {
        Map<String, Object> modelMap = new HashMap<>();
        List<User> userList = userService.getUsersByKeyword(keyword);
        List<Boolean> alreadyFollowList = new ArrayList<>();
        for (User user : userList
        ) {
            alreadyFollowList.add(postService.ifAlreadyFollow(user.getUser_id(), follow_follower));
        }
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", userList);
        modelMap.put("follow", alreadyFollowList);
        return modelMap;
    }

    @RequestMapping(value = "getUserById", method = RequestMethod.POST)
    private Map<String, Object> getUserById(Integer user_id) {
        Map<String, Object> modelMap = new HashMap<>();
        User user = userService.getUserById(user_id);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", user);
        return modelMap;
    }

    @RequestMapping(value = "getAuthorById", method = RequestMethod.POST)
    private Map<String, Object> getAuthorById(Integer user_id) {
        Map<String, Object> modelMap = new HashMap<>();
        User user = userService.getAuthorById(user_id);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", user);
        return modelMap;
    }


    @RequestMapping(value = "getStsToken", method = RequestMethod.GET)
    private Map<String, Object> getStsToken() {
        Map<String, Object> modelMap = new HashMap<>();
        AssumeRoleResponse response = stsService.getSTSResponse();
        System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
        System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
        System.out.println("Security Token: " + response.getCredentials().getBizSecurityToken());
        modelMap.put("accesskeyId", response.getCredentials().getAccessKeyId());
        modelMap.put("accessKeySecret", response.getCredentials().getAccessKeySecret());
        modelMap.put("stsToken", response.getCredentials().getBizSecurityToken());
        modelMap.put("expiration", response.getCredentials().getExpiration());
        modelMap.put("code", "0");
        return modelMap;
    }

    @RequestMapping(value = "updateAvator", method = RequestMethod.POST)
    private Map<String, Object> updateAvator(Integer user_id, Integer user_avator) {
        Map<String, Object> modelMap = new HashMap<>();
        if (userService.updateAvator(user_id, user_avator)) {
            modelMap.put("code", "0");
        } else
            modelMap.put("code", "-1");
        return modelMap;
    }

    //    @CrossOrigin(origins = "http://[::1]:54456", maxAge = 3600)
    @RequestMapping(value = "getAllStudent", method = RequestMethod.GET)
    private Map<String, Object> getAllStudent() {
        Map<String, Object> modelMap = new HashMap<>();
        List<User> users = userService.getAllStudent();
        modelMap.put("code", "0");
        modelMap.put("data", users);
        return modelMap;
    }

    @RequestMapping(value = "deleteStudent", method = RequestMethod.POST)
    private Map<String, Object> deleteStudent(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (userService.deleteStudent(map.get("user_username").toString()))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }


    @RequestMapping(value = "updateStudent", method = RequestMethod.POST)
    private Map<String, Object> updateStudent(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        User user = new User(map.get("user_username").toString(), map.get("user_truename").toString(), map.get("user_dorm_building").toString(),
                map.get("user_dorm_room").toString(), map.get("user_class_college").toString(), map.get("user_class_major").toString(), map.get("user_class_number").toString());
        if (userService.updateStudent(user))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "updateInfo", method = RequestMethod.POST)
    private Map<String, Object> updateInfo(String type, String newValue, Integer user_id) {
        Map<String, Object> modelMap = new HashMap<>();
        if (userService.updateInfo(type, newValue, user_id))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "updatePassword", method = RequestMethod.POST)
    private Map<String, Object> updatePassword(String user_username, String user_password){
        Map<String, Object> modelMap = new HashMap<>();
        if (userService.updatePassword(user_username, user_password))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "resetPassword", method = RequestMethod.POST)
    private Map<String, Object> resetPassword(@RequestBody Map<String, Object> map){
        Map<String, Object> modelMap = new HashMap<>();
        if (userService.resetPassword(map.get("user_username").toString()))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "resetPasswordUser", method = RequestMethod.POST)
    private Map<String, Object> resetPasswordUser(String user_username, String user_truename, String user_phone){
        Map<String, Object> modelMap = new HashMap<>();
        if (userService.resetPasswordUser(user_username, user_truename, user_phone))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

}
